package com.whfc.ms.api.base.service.impl;

import com.whfc.base.dto.MsgRecordDTO;
import com.whfc.base.dto.MsgStatisticsDTO;
import com.whfc.base.param.WarnMsgSearchParam;
import com.whfc.base.service.MessageService;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.PageVO;
import com.whfc.entity.dto.msg.AppMsgNumDTO;
import com.whfc.entity.dto.msg.AppMsgToUserDTO;
import com.whfc.entity.dto.msg.AppWarnMsgDTO;
import com.whfc.entity.param.msg.AppMsgDelAllParam;
import com.whfc.entity.param.msg.AppMsgDelParam;
import com.whfc.entity.param.msg.AppMsgReadAllParam;
import com.whfc.entity.param.msg.AppMsgReadParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysUserService;
import com.whfc.ms.api.base.service.AppMessageService;
import com.whfc.quality.dto.QualityCheckDTO;
import com.whfc.quality.dto.QualityCheckDescDTO;
import com.whfc.quality.enums.QualityCheckUserType;
import com.whfc.quality.param.QualityCheckListParam;
import com.whfc.quality.service.QualityCheckService;
import com.whfc.safety.dto.SafetyCheckDTO;
import com.whfc.safety.dto.SafetyCheckDescDTO;
import com.whfc.safety.enums.SafetyCheckUserType;
import com.whfc.safety.param.SafetyCheckListParam;
import com.whfc.safety.service.SafetyCheckService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author sun_guodong
 * @description
 * @date 2019-12-05
 */
@Service
public class AppMessageServiceImpl implements AppMessageService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @DubboReference(interfaceClass = SysUserService.class, version = "1.0.0")
    private SysUserService sysUserService;

    @DubboReference(interfaceClass = MessageService.class, version = "1.0.0")
    private MessageService messageService;

    @DubboReference(interfaceClass = QualityCheckService.class, version = "1.0.0")
    private QualityCheckService qualityCheckService;

    @DubboReference(interfaceClass = SafetyCheckService.class, version = "1.0.0")
    private SafetyCheckService safetyCheckService;

    @Override
    public ListData<AppMsgToUserDTO> getAvailableRecUsers(Integer deptId) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        List<AppMsgToUserDTO> list = sysUserService.getAvailableRecUsers(deptIds);
        return new ListData<>(list);
    }

    @Override
    public PageData<AppWarnMsgDTO> list(SysUser user, Integer pageNum, Integer pageSize, Integer deptId, Integer state, Integer moduleType) {
        return messageService.list(user, pageNum, pageSize, deptId, state, moduleType);
    }

    @Override
    public AppMsgNumDTO getUnreadMsgNum(SysUser user, Integer deptId) {
        return messageService.getUnreadMsgNum(user, deptId);
    }

    @Override
    public void del(AppMsgDelParam request) {
        List<Integer> idList = request.getIdList();
        if (idList != null && idList.size() > 0) {
            messageService.del(idList);
        }
    }

    @Override
    public void delAll(SysUser user, AppMsgDelAllParam request) {
        String phone = user.getPhone();
        Integer deptId = request.getDeptId();
        Integer moduleType = request.getModuleType();
        Integer state = request.getState();
        messageService.delAll(phone, deptId, moduleType, state);
    }

    @Override
    public void read(AppMsgReadParam request) {
        List<Integer> idList = request.getIdList();
        if (idList != null && idList.size() > 0) {
            messageService.read(idList);
        }

    }

    @Override
    public void readAll(SysUser user, AppMsgReadAllParam request) {
        String phone = user.getPhone();
        Integer deptId = request.getDeptId();
        Integer moduleType = request.getModuleType();
        messageService.readAll(phone, deptId, moduleType);
    }

    @Override
    public Map getNewApproveWithMe(Integer userId,Integer deptId) {
        // 质量
        QualityCheckListParam checkListParam = new QualityCheckListParam();
        checkListParam.setUserId(userId);
        checkListParam.setPageSize(3);
        checkListParam.setPageNum(1);
        checkListParam.setDeptId(deptId);
        Integer[] type = {QualityCheckUserType.RECTIFIER.getValue(), QualityCheckUserType.REVIEWER.getValue(), QualityCheckUserType.VERIFIER.getValue()};
        checkListParam.setType(type);
        List<QualityCheckDTO> qualityCheckList = qualityCheckService.list(checkListParam).getCheckList();
        List<Integer> checkIds = qualityCheckList.stream().map(QualityCheckDTO::getCheckId).collect(Collectors.toList());
        List<QualityCheckDescDTO> descList = qualityCheckService.descByIds(checkIds);

        // 安全
        SafetyCheckListParam safetyCheckListParam = new SafetyCheckListParam();
        Integer[] safetyType = {SafetyCheckUserType.RECTIFIER.getValue(), SafetyCheckUserType.REVIEWER.getValue(), SafetyCheckUserType.VERIFIER.getValue()};
        safetyCheckListParam.setUserId(userId);
        safetyCheckListParam.setPageSize(3);
        safetyCheckListParam.setPageNum(1);
        safetyCheckListParam.setDeptId(deptId);
        safetyCheckListParam.setType(safetyType);
        List<SafetyCheckDTO> safetyCheckList = safetyCheckService.list(safetyCheckListParam).getCheckList();
        List<Integer> safetyCheckIds = safetyCheckList.stream().map(SafetyCheckDTO::getCheckId).collect(Collectors.toList());
        List<SafetyCheckDescDTO>  safetyDescList = safetyCheckService.descByIds(safetyCheckIds);

        HashMap result = new HashMap<>(3);
        result.put("count",descList.size()+safetyDescList.size());
        result.put("qualityList",descList);
        result.put("safetyList",safetyDescList);
        return result;
    }

    @Override
    public List<MsgStatisticsDTO> getWarnMsgStatistics(Integer deptId, Integer warnType, Integer userId) {
        return messageService.getWarnMsgStatistics(deptId,warnType,userId);
    }

    @Override
    public PageVO<MsgRecordDTO> getWarnMsgPage(WarnMsgSearchParam param) {
        return messageService.getWarnMsgPage(param);
    }
}
